package org.java.demo;

import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.util.Arrays;
import java.util.regex.Pattern;

public class Demo2 {
    public static void main(String[] args) throws FileNotFoundException {
        //读取excel
        File file = new File("F:\\东吴人寿\\20250219\\FICostDataAcquisitionDef.xlsx");
        FileInputStream fis = new FileInputStream("F:\\东吴人寿\\20250219\\FICostDataAcquisitionDef.xlsx");
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\东吴人寿\\20250219\\test.txt");
        PrintStream printStream = new PrintStream(fileOutputStream);
        try {
            Workbook workbook = WorkbookFactory.create(fis);
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row:sheet){
                int rowNum = row.getRowNum();
                for (Cell cell:row){
                    String stringCellValue = cell.getStringCellValue();
                    int columnIndex = cell.getColumnIndex();
                    //当前行为第一行则是标题
                    if(rowNum == 0){

                    }else{
                        if(columnIndex == 6){
                            //提取出主表并打印,正则不区分大小写
                            Pattern compile = Pattern.compile(" as ", Pattern.CASE_INSENSITIVE);
                            String[] froms = compile.split(stringCellValue);
                            String asString = froms[froms.length - 1];
                            //在截取from,正则不区分大小写
                            Pattern fmCom = Pattern.compile(" from ", Pattern.CASE_INSENSITIVE);
                            String[] split = fmCom.split(asString);

                            //在截取where,正则不区分大小写
                            Pattern whCom = Pattern.compile(" where", Pattern.CASE_INSENSITIVE);
                            String[] whSp = whCom.split(split[1]);

                            printStream.println(whSp[0]);
                            System.out.println(whSp[0]);
                            //System.out.println("*************************************************************************");
                        }
                    }
                }
            }


        } catch (Exception e) {
            printStream.flush();
            printStream.close();
            throw new RuntimeException(e);
        }
        printStream.flush();
        printStream.close();
    }
}
